//
//  WeekView.m
//  MindBox
//
//  Created by MS on 16-1-4.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#import "WeekView.h"
#import "Week_ItemView.h"
#import "UIView+Frame.h"

@interface WeekView ()
@property (weak, nonatomic) IBOutlet UIView *headerView;

@property (nonatomic,strong) NSMutableArray * itemArray;

@end


@implementation WeekView

-(NSMutableArray *)itemArray
{
    if (_itemArray==nil) {
        _itemArray = [[NSMutableArray alloc] init];
    }
    return _itemArray;
}

+(instancetype)weekViewWithXib
{
     return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] lastObject];
}


-(void)setWeeks:(NSArray *)weeks
{
    _weeks = weeks;
    int i =0;
    for (Week * week in weeks) {
        Week_ItemView * item = self.itemArray[i];
        
        if (i==0) {
            item.titleView.numberOfLines = 2;
            item.titleView.font =[UIFont systemFontOfSize:11];
            item.priceView.font =[UIFont systemFontOfSize:10];
        }
        else{
            item.titleView.numberOfLines = 1;
        }
        item.week = week;
        i++;
    }
}

-(void)awakeFromNib
{
    self.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
    
    for (int i =0; i<5; i++) {
        Week_ItemView * itemView = [Week_ItemView week_itemViewWithXib];
        itemView.tag = i;
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
        [itemView addGestureRecognizer:tap];
        [self.itemArray addObject:itemView];
        [self addSubview:itemView];
    }

}

-(void)tapClick:(UIGestureRecognizer *)tap
{
    NSInteger index = tap.view.tag;
    
    if ([self.delegate respondsToSelector:@selector(weekViewDidClick:week:index:)]) {
        [self.delegate weekViewDidClick:self week:self.weeks[index] index:index];
    }
}

- (IBAction)btnClick:(id)sender {
    
    if ([self.delegate respondsToSelector:@selector(weekViewDidClick:week:index:)]) {
        [self.delegate weekViewDidClick:self week:nil index:-1];
    }
    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
     int margin = 1;
    
    CGFloat w =self.width/3.5;
    CGFloat y0 = CGRectGetMaxY(self.headerView.frame);
    CGFloat x0 = 1.5*w;
    

    CGFloat h =(self.height-y0-2*margin)*0.5;
    int i = 0;
    for (Week_ItemView * item in self.itemArray) {
        
        if (i==0) {
            item.frame = CGRectMake(0, y0+margin, 1.5*w, self.height-y0);
        }
        else{
            //列
            int col = (i-1)%2;
            //行
            int rol = (i-1)/2;
        
            CGFloat x = x0 + margin+(margin+w)*col;
            CGFloat y = y0 + margin+(margin+h)*rol;
            item.frame = CGRectMake(x, y, w, h);
        }
        
        i++;
    }

}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
